package com.lezheba.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.lezheba.db.Dao;
import com.lezheba.db.entity.Category;
import com.lezheba.db.entity.City;
import com.lezheba.db.entity.Deal;
import com.lezheba.db.entity.Shop;
import com.lezheba.db.entity.User;

public class IndexService {
	static Logger logger = Logger.getLogger(IndexService.class.getName());

	private Dao dao = Dao.getInstance();

	public boolean checkUser(String email, String password) {
		String hql = "from User where email=? and password=?";
		return dao.getListByHql(hql, new Object[] { email, password }).size() > 0;
	}

	public boolean isExsitUser(String email) {
		String hql = "from User where email=?";
		return dao.getListByHql(hql, new Object[] { email }).size() > 0;
	}

	public User saveUser(String email, String password) {
		if (email != null && password != null) {
			User u = new User();
			u.setEmail(email);
			u.setPassword(password);
			dao.saveOrUpdate(u);
			return u;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Deal> getDealList(String cityName, long cateId, String key,
			int page, int limit) {
		String hql = "from Deal where ";
		Object[] values = null;
		List paras = bulidHql(cityName, cateId, key);
		if (paras.size() > 0) {
			hql = hql + paras.get(paras.size() - 1);
			values = paras.subList(0, paras.size() - 1).toArray();
		}
		List<Deal> list = dao.getListByHqlPage(hql, values, page, limit);
		return list;
	}

	public long getDealtotal(String cityName, long cateId, String key) {
		String hql = "select count(*) from Deal where  ";
		Object[] values = null;
		List paras = bulidHql(cityName, cateId, key);
		if (paras.size() > 0) {
			hql = hql + paras.get(paras.size() - 1);
			values = paras.subList(0, paras.size() - 1).toArray();
		}
		return (Long) dao.getObjectByHql(hql, values);
	}

	private List bulidHql(String cityName, long cateId, String key) {
		List list = new ArrayList<Object>();
		list.add(new Long(getNow()));

		StringBuilder hql = new StringBuilder(" endTime>? ");

		if (cityName != null && cityName.length() > 1) {
			hql.append(" and cityName like ?");
			list.add("%" + cityName + "%");
		}

		if (cateId > 0) {
			hql.append(" and dealCateId=?");
			list.add(cateId);
		}

		if (key != null && key.length() > 1) {
			hql.append(" and (dealDesc like ? or dealTitle like ? or dealTips like ?)");
			list.add("%" + key + "%");
			list.add("%" + key + "%");
			list.add("%" + key + "%");
		}
		if (list.size() > 0) {
			list.add(hql.toString());
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<City> getCityList() {
		String hql = "from City where cityId is not null order by cityId ";
		List<City> list = dao.getListByHql(hql);
		return list;
	}

	public TreeMap<String, List<City>> getCityMap() {
		TreeMap<String, List<City>> map = new TreeMap<String, List<City>>();
		List<City> list = getCityList();
		Set cityIdSet = new HashSet<String>();
		for (City city : list) {

			String cityId = city.getCityId();
			if (cityId != null) {
				if (!cityIdSet.contains(cityId)) {
					cityIdSet.add(cityId);
					String key = cityId.substring(0, 1).toUpperCase();
					if (!map.containsKey(key)) {
						map.put(key, new ArrayList<City>());
					}
					map.get(key).add(city);
				}
			}

		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public List<Deal> getDealList(String cityName, long cateId, int page,
			int limit) {
		String hql = "from Deal where dealCateId=? and endTime>? and cityName like ? ";

		if (cityName != null && cityName.endsWith("市")) {
			cityName = cityName.substring(0, cityName.length() - 1);
		}

		List<Deal> list = dao.getListByHqlPage(hql, new Object[] { cateId,
				new Long(getNow()), "%" + cityName + "%" }, page, limit);
		return list;
	}

	public List<Deal> getDealListByCityId(String cityId, long cateId, int page,
			int limit) {
		String cityName = getCityName(cityId);
		logger.info("cityName" + cityName);
		return getDealList(cityName, cateId, page, limit);
	}

	public long getDealtotal(String cityName, long cateId) {
		String hql = "select count(*) from Deal where dealCateId=? and endTime>? and cityName like ? ";

		if (cityName != null && cityName.endsWith("市")) {
			cityName = cityName.substring(0, cityName.length() - 1);
		}
		return (Long) dao.getObjectByHql(hql, new Object[] { cateId,
				new Long(getNow()), "%" + cityName + "%" });
	}

	public List<Category> getCategoryList() {
		String hql = "from Category order by name";
		@SuppressWarnings("unchecked")
		List<Category> list = dao.getListByHql(hql, null);
		return list;
	}

	public Shop getShop(long dealId) {
		return (Shop) dao.getObjectByHql("from Shop where dealId=?", dealId);
	}

	public String getCityName(String cityId) {
		return (String) dao.getObjectByHql(
				"select name from City where cityId=?", cityId);
	}

	private long getNow() {
		// return 0;
		return (new Date()).getTime() / 1000;
	}

	public static void main(String[] args) {

		IndexService service = new IndexService();
		/*
		 * List<Deal> dealList = service.getDealList("深圳"); for(Deal deal :
		 * dealList){ System.out.println("deal:" + deal); }
		 */
		System.out.println((new Date()).getTime() / 1000);
		System.setProperty("file.encoding", "GB18030");
		List<Category> dealList = service.getCategoryList();
		List<Deal> deals = service.getDealList("guangzhou", 6, 1, 10);
		// getDealList2("", 0, "", 1, 10);
		for (Deal deal : deals) {
			System.out.println("deal:" + deal);
		}
		System.out.println(System.getProperty("file.encoding"));

		// logger.error("dealList size:" + dealList.size());
		// TODO Auto-generated method stub

	}

}
